<template>
  <div class="user-info">
    <el-form 
      :model="userForm" 
      :rules="rules" 
      ref="formRef" 
      label-width="100px"
      class="user-form"
    >
      <el-form-item label="用户名">
        <el-input v-model="userForm.username" disabled />
      </el-form-item>
      <el-form-item label="姓名" prop="full_name">
        <el-input v-model="userForm.full_name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="userForm.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="角色">
        <el-tag :type="userForm.role === 'admin' ? 'danger' : 'info'">
          {{ userForm.role === 'admin' ? '管理员' : '普通用户' }}
        </el-tag>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit" :loading="loading">保存修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch, defineProps, defineEmits } from 'vue';
import { ElMessage, ElForm } from 'element-plus';
import { updateUser } from '@/api/user';

// 定义组件接受的属性
const props = defineProps({
  userInfo: {
    type: Object,
    required: true
  }
});

// 定义组件触发的事件
const emit = defineEmits(['refresh']);

// 表单引用
const formRef = ref<InstanceType<typeof ElForm>>();

// 加载状态
const loading = ref(false);

// 用户表单数据
const userForm = reactive({
  _id: '',
  username: '',
  full_name: '',
  email: '',
  role: ''
});

// 表单验证规则
const rules = {
  full_name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { max: 50, message: '姓名长度不能超过50个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
};

// 监听props变化
watch(() => props.userInfo, (newValue) => {
  if (newValue) {
    // 更新表单数据
    userForm._id = newValue._id || '';
    userForm.username = newValue.username || '';
    userForm.full_name = newValue.full_name || '';
    userForm.email = newValue.email || '';
    userForm.role = newValue.role || '';
  }
}, { immediate: true, deep: true });

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        // 准备更新数据
        const updateData = {
          full_name: userForm.full_name,
          email: userForm.email
        };
        
        // 调用API更新数据
        const res: any = await updateUser(userForm._id, updateData);
        if (res.code === 200) {
          ElMessage.success('个人信息更新成功');
          // 通知父组件刷新数据
          emit('refresh');
        } else {
          ElMessage.error(res.message || '更新失败');
        }
      } catch (error) {
        console.error('更新个人信息错误:', error);
        ElMessage.error('更新个人信息出错');
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style lang="scss" scoped>
.user-info {
  padding: 20px;
  
  .user-form {
    max-width: 600px;
  }
}
</style>